//加载字典
function offlineTypeDict(value, options, row){
    //sex为自定义字典项
	var statusObj = vm.offline_type;
    for( var i=0;i< statusObj.length;i++ ){
        if(statusObj[i].value == value){
            return '<span>'+statusObj[i].text+'</span>' ;
        }
    }
}
//加载字典
function jclxTypeDict(value, options, row){
    //sex为自定义字典项
    var statusObj = vm.jclx;
    for( var i=0;i< statusObj.length;i++ ){
        if(statusObj[i].value == value){
            return '<span>'+statusObj[i].text+'</span>' ;
        }
    }
}
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'offlinestudy/subscribedate/subscribeList',
        datatype: "json",
        postData:{"subscribetype":'2'},
        colModel: [
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden :true},
			{ label: '', name: 'userid', index: 'userid', width: 80,hidden:true },
			{ label: '', name: 'subscribedateId', index: 'subscribedate_id', width: 80,hidden:true },
			{ label: '学习类型', name: 'studytype', index: 'studytype', width: 60,formatter:offlineTypeDict },
			{ label: '', name: 'subscribetype', index: 'subscribetype', width: 80,hidden:true },
			{ label: '', name: 'address', index: 'address', width: 80 ,hidden:true},
			{ label: '姓名', name: 'name', index: 'name', width: 80 },
			{ label: '身份证号', name: 'sfzh', index: 'sfzh', width: 120 },
			{ label: '档案编号', name: 'dabh', index: 'dabh', width: 80 },
			{ label: '驾车类型', name: 'jclx', index: 'jclx', width: 80,formatter:jclxTypeDict },
			{ label: '电话', name: 'tel', index: 'tel', width: 80 },
			{ label: '', name: 'fj', index: 'fj', width: 80,hidden:true },
			{ label: '录入时间', name: 'ldate', index: 'ldate', width: 80,formatter:formatSubscribeTime },
			{ label: '', name: 'subscribedateTypeid', index: 'subscribedate_typeid', width: 80 ,hidden:true},
			{ label: '学习开始日期', name: 'startdate', index: 'startdate', width: 80,formatter:formatDate },
			{ label: '学习结束日期', name: 'enddate', index: 'enddate', width: 80,formatter:formatDate }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		subscribe: {},
        offline_type:[],
        jclx:[],
        varJclx:'',
        varStudytype:'',
        subscribedate:[],
        varSubscribedate:'',
        queryCondition:{"studytype":''}
	},
    created:function() {
        $.get(baseURL+"/sys/dict/select/offline_type",function(r){
            if(r.code == 0) {
                vm.offline_type = r.dict.offline_type;
            }else{
                console.log(r.msg);
            }
        });
        $.get(baseURL+"/sys/dict/select/jclx",function(r){
            if(r.code == 0) {
                vm.jclx = r.dict.jclx;
            }else{
                console.log(r.msg);
            }
        });
    },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.subscribe = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    if(!vm.checkInput()) return false;
		    vm.subscribe.studytype = vm.varStudytype;
		    vm.subscribe.jclx = vm.varJclx;
			var url = vm.subscribe.id == null ? "offlinestudy/subscribedate/subscribeSave" : "offlinestudy/subscribedate/subscribeUpdate";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.subscribe),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "offlinestudy/subscribedate/subscribeDelete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "offlinestudy/subscribedate/subscribeInfo/"+id, function(r){
                vm.subscribe = r.subscribe;
                vm.varSubscribedate = vm.subscribe.subscribedate;
                vm.varJclx=vm.subscribe.jclx;
                vm.varStudytype = vm.subscribe.studytype;
                vm.varSubscribedate = formatDate(vm.subscribe.startdate)+"至"+formatDate(vm.subscribe.enddate);
            });
		},
		reload: function (event) {
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
            $.each(postData, function (k, v) {
                delete postData[k];
            });
			vm.showList = true;
            postData = {
                "name":vm.queryCondition.name==null?undefined:vm.queryCondition.name,
                "sfzh":vm.queryCondition.sfzh==null?undefined:vm.queryCondition.sfzh,
                "studytype":vm.queryCondition.studytype==null?undefined:vm.queryCondition.studytype,
                "subscribetype":'2'
            };
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                page:page,
                postData:postData
            }).trigger("reloadGrid");
		},
        subscribedateChecked: function(subscribedate_id){
		    $.each(vm.subscribedate,(index,item)=>{
                if(item.id == subscribedate_id) {
                    vm.subscribe.subscribedateId=subscribedate_id;
                    vm.subscribe.address = item.address;
                    vm.subscribe.subscribetype = '2';
                    vm.subscribe.ldate = new Date();
                    vm.subscribe.subscribedateTypeid = item.typeid;
                    vm.subscribe.startdate = item.startdate;
                    vm.subscribe.enddate = item.enddate;
                    vm.varSubscribedate = item.subscribedate;
                    return ;
                }
            });
        },
        checkInput: function(){
            if(!vm.subscribe.name){
                alert("请输入姓名！");
                return false;
            }
            if(!vm.subscribe.sfzh) {
                alert("请输入身份证号！");
                return false;
            }else{
                var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
                if(reg.test(vm.subscribe.sfzh) === false)
                {
                    alert("身份证输入不合法，请重填！");
                    return false;
                }
            }
            if(!vm.varStudytype) {
                alert("请选择学习类型！");
                return false;
            }
            if(!vm.subscribe.tel) {
                alert("请填写手机号！");
                return false;
            }else{
                if(!(/^1[345789]\d{9}$/.test(vm.subscribe.tel))){
                    alert("手机号码有误，请重填!");
                    return false;
                }
            }
            if(!vm.varJclx){
                alert("请选择准驾车型！");
                return false;
            }
            if(!vm.subscribe.dabh){
                alert("请填写档案编号！");
                return false;
            }
            if(!vm.varSubscribedate){
                alert("请选择学习时间！");
                return false;
            }
            return true;
        },
        checkStudytype:function(){
            if(vm.subscribedate.length ==0 ){
                alert("请先选择学习类型！");
                event.preventDefault();
                console.log(event);
            }
        },
        studyOnchange:function() {
            vm.varSubscribedate='';
            $.ajax({
                type: "POST",
                url: baseURL + "offlinestudy/subscribedate/selectValidateSubscribeDate",
                data: {"studytype":vm.varStudytype},
                success: function(r){
                    if(r.code == 0){
                        vm.subscribedate = r.list;
                    }else{
                        alert(r.msg);
                    }
                }
            });
        }
	}
});

function formatDate(cellValue) {
    var dateStr = "";
    if(cellValue){
        var crtTime = new Date(cellValue);
        dateStr = dateFtt("yyyy-MM-dd",crtTime);
    }
    return dateStr;
}